<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/9
 * Time: 16:07
 */
namespace app\api\controller;
use think\Controller;
use think\Db;
import('wxpay.WxPay_Api', EXTEND_PATH);
import('wxpay.WxPay_Notify', EXTEND_PATH);
import('wxpay.WxPay_Sign', EXTEND_PATH);
import('wxpay.log', EXTEND_PATH);



class Wxrtn{
    public function wxnotify(){
        //初始化日志
        if (!is_dir(ROOT_PATH.'./runtime/logs/')) {
            mkdir(ROOT_PATH.'./runtime/logs/', 0755, true);
        }
        echo 'weChatPayRtn';
        $logHandler= new \CLogFileHandler(ROOT_PATH."./runtime/logs/".date('Y-m-d').'.log');
        $log = \Log::Init($logHandler, 15);
        $notify = new PayNotifyCallBack();
        $log->DEBUG('begin notify');
        $notify->Handle(true);
    }
}

class PayNotifyCallBack extends \WxPayNotify
{
    //查询订单
    public function Queryorder($transaction_id)
    {
        $input = new \WxPayOrderQuery();
        $input->SetTransaction_id($transaction_id);
        $result = \WxPayApi::orderQuery($input);
        if (array_key_exists("return_code", $result)
            && array_key_exists("result_code", $result)
            && $result["return_code"] == "SUCCESS"
            && $result["result_code"] == "SUCCESS"
        ) {
            return true;
        }
        return false;
    }

    //重写回调处理函数
    public function NotifyProcess($data, &$msg)
    {

        $logHandler = new \CLogFileHandler(ROOT_PATH."./runtime/logs/" . date('Y-m-d') . '.log');
        $log = \Log::Init($logHandler, 15);

        if (!array_key_exists("transaction_id", $data)) {
            $msg = "输入参数不正确";
            $log->DEBUG($msg);
            return false;
        }
        //查询订单，判断订单真实性
        if (!$this->Queryorder($data["transaction_id"])) {
            $msg = "订单查询失败";
            $log->DEBUG($msg);
            return false;
        }

        $money = $data['total_fee'];                  //金额
        $order_no = $data['out_trade_no'];            //订单编号

        //充值订单查询
        $recharge_where['rrd.recharge_no'] = $order_no;

        $now_date = date('Y-m-d',time());
        $recharge_where['at.begin_date'] = array('elt',$now_date);
        $recharge_where['at.end_date'] = array('egt',$now_date);

        $recharge_info = Db::name('recharge_record')->alias('rrd')
            //关联充值优惠
            ->join('activity at','rrd.area_id = at.area_id','left')
            ->field('rrd.passenger_id,rrd.recharge_record_id,rrd.recharge_no,rrd.is_success,at.recharge_price,at.give_price')
            ->where($recharge_where)->find();

        //用车订单完成 -- 支付完成
        $order_info = Db::name('order')->where('order_no',$order_no)->find();

        if ($recharge_info) {

            $log->DEBUG('recharge_id:' . $recharge_info['recharge_record_id']);
            $log->DEBUG('recharge_no:' . $recharge_info['recharge_no']);

            if ($recharge_info['is_success'] == '9' && is_numeric($money)) {

                if($recharge_info['recharge_price'] && $money >= $recharge_info['recharge_price']){

                    //充值金额加上赠送金额
                    $give_price = $recharge_info['give_price'];
                    $re_data['give_money'] = $give_price;
                    $money = $money + $give_price;
                }
                $re_data['is_success'] = '1';
                Db::name('recharge_record')->where('recharge_no',$order_no)->update($re_data);
                Db::name('passenger')->where('passenger_id',$recharge_info['passenger_id'])->Inc('balance',$money)->update();

                return true;//充值成功
            }
        } elseif ($order_info) {

            $log->DEBUG('order_id:' . $order_info['order_id']);
            $log->DEBUG('order_no:' . $order_info['order_no']);

            if ($recharge_info['order_state'] == '8' && is_numeric($money)) {

                Db::name('order')->where('order_no',$order_no)->setField('order_state',10);

                return true;//支付成功
            }
        }
        //支付失败
        $log->DEBUG('success:');
        $log->DEBUG('out_trade_no:' . $data['out_trade_no']);
        $success = false;
        return $success;//是否支付成功
    }
}